package cn.iocoder.an.module.mold.service.upkeepitem;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepcat2item.UpkeepCat2itemDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.an.module.mold.controller.admin.upkeepitem.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepitem.UpkeepItemDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.mold.dal.mysql.upkeepitem.UpkeepItemMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 模具保养项目 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class UpkeepItemServiceImpl implements UpkeepItemService {

    @Resource
    private UpkeepItemMapper upkeepItemMapper;



    @Override
    public Long createUpkeepItem(UpkeepItemSaveReqVO createReqVO) {
        // 插入
        UpkeepItemDO upkeepItem = BeanUtils.toBean(createReqVO, UpkeepItemDO.class);
        upkeepItemMapper.insert(upkeepItem);

        // 返回
        return upkeepItem.getId();
    }

    @Override
    public void updateUpkeepItem(UpkeepItemSaveReqVO updateReqVO) {
        // 校验存在
        validateUpkeepItemExists(updateReqVO.getId());
        // 更新
        UpkeepItemDO updateObj = BeanUtils.toBean(updateReqVO, UpkeepItemDO.class);
        upkeepItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteUpkeepItem(Long id) {
        // 校验存在
        validateUpkeepItemExists(id);
        // 删除
        upkeepItemMapper.deleteById(id);
    }

    @Override
    public void deleteUpkeepItemListByIds(List<Long> ids) {
        // 删除
        upkeepItemMapper.deleteByIds(ids);
    }


    private void validateUpkeepItemExists(Long id) {
        if (upkeepItemMapper.selectById(id) == null) {
            throw exception(UPKEEP_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public UpkeepItemDO getUpkeepItem(Long id) {
        return upkeepItemMapper.selectById(id);
    }

    @Override
    public PageResult<UpkeepItemDO> getUpkeepItemPage(UpkeepItemPageReqVO pageReqVO) {
        return upkeepItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UpkeepItemDO> getUpkeepItemAll( ) {
        return upkeepItemMapper.selectList();
    }

    @Override
    public List<UpkeepItemDO> getUpkeepItemByIds(List<Long> ids) {
        return upkeepItemMapper.selectBatchIds(ids);
    }

}